import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import store from '@/store/index'

Vue.use(VueRouter)

const routes = [



  {
    path: '/',
    name: 'login',
    component: Login,

  },
  {
    path: '/index',
    name: 'index',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import('../views/HomePage.vue'),
    children: [{
      path: '',
      name: 'echarts',
      component: () => import('../views/children/echarts.vue')
    },
    {
      path: 'userinfo',
      name: 'userinfo',
      component: () => import('../views/children/UserInfo.vue')
    },
    {
      path: 'yishi',
      name: 'yishi',
      component: () => import('../views/children/Yishi.vue')
    },
    {
      path: 'user',
      name: 'user',
      component: () => import('../views/children/User.vue')
    },
    {
      path: 'table',
      name: 'table',
      component: () => import('../views/children/Table.vue')
    },
    {
      path: 'room',
      name: 'room',
      component: () => import('../views/children/Room.vue')
    },
    {
      path: 'pwd',
      name: 'pwd',
      component: () => import('../views/children/Pwd.vue')
    },
    {
      name: 'age',
      path: 'age',
      component: () => import('../views/children/Age.vue')
    },
    {
      path: 'admin',
      name: 'admin',
      component: () => import('../views/children/Admin.vue')
    },
    {
      path: 'edit',
      name: 'edit',
      component: () => import('../views/children/Edit.vue')
      },
    {
      path: 'user-avatar',
      component: () => import('../views/children/userAvatar.vue')
    }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  // store.dispatch('getuserInfoActions')
  const token = localStorage.getItem('token')
  if (to.name == 'index' && !token) next({ name: 'login' })


  next()
})
export default router
